package control
{
	import data.DataBase;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import stage.Stage_lv1;

	public class MonsterMove extends Sprite
	{
		public function MonsterMove(dat:DataBase,_monsterArr:Array)
		{
			lineArray = dat.line_arr;
			monsterArr = _monsterArr;
			crtPoint.x = lineArray[0][0]+50;
			crtPoint.y = lineArray[0][1]-20;
			move()
		}
		/**
		 * 全部怪物数组
		 */		
		private var monsterArr:Array
		/**
		 *路线数组 
		 */		
		private var lineArray:Array;
		/**
		 *移动速度	timer速度 
		 */		
		public var speed:uint = 10;
		/**
		 *移动速度	算法速度 
		 */		
		public var speed2:Number = 1.2;
		/**
		 *怪物的方向和动作    0:左,1:下,2:右,3:上,4:死亡,5:攻击
		 */		
		public var direction:uint;
		/**
		 *当前怪物的位置 
		 */		
		public var crtPoint:Point = new Point();
		
		private var randomX:int = 20-Math.random()*40;
		private var randomY:int = 20-Math.random()*40;
		private var _x:Number;
		private var _y:Number;
		private var dx:Number;
		private var dy:Number;
		private var dist:Number;
		private var index:uint = 0;

		private var myTimer:Timer;
		/**
		 *移除 
		 * 
		 */		
		public function remove():void
		{
			myTimer.stop();
			myTimer.removeEventListener(TimerEvent.TIMER,movingStart)
			myTimer = null
			crtPoint = null
			lineArray = null
			direction = 0;
			randomX = 0;
			randomY = 0;
			speed = 0;
			speed2 = 0;
			_x =0;
			_y=0;
			dy = 0;
			dx = 0;
			dist = 0;
			index = 0;
			removeFromArray();
			monsterArr =null
				
		}
		private function removeFromArray():void
		{
			for(var i:uint = 0; i<monsterArr.length;i++)
			{
				if(monsterArr[i].crtPoint == null)
				{
					monsterArr.splice(i,1);
					
				}
			}
		}
		
		
		
		/**
		 *开始移动 
		 * 
		 */		
		private function move():void
		{
			myTimer=new Timer(speed);
			myTimer.start();
			myTimer.addEventListener(TimerEvent.TIMER,movingStart)
		}
		/**
		 * 检测下一个点
		 * @param event
		 * 
		 */		
		protected function movingStart(event:TimerEvent):void
		{
			moving();
			if(dist <10)
			{
				index++
			}
			if(index>lineArray.length-1)
			{
				//remove();
				myTimer.stop();
				direction =4;
				Stage_lv1.updateMainHp(-1)
			}
		}
		/**
		 * 更新crtPoint
		 * 
		 */		
		private function moving():void
		{
			if(index==lineArray.length-1)
			{
				return;
			}
			dx = lineArray[index+1][0]+randomX-crtPoint.x
			dy = lineArray[index+1][1]+randomY-crtPoint.y-20
			dist = Math.sqrt(dx*dx+dy*dy);
			_x = Math.cos(Math.atan2(dy,dx))*speed2;
			_y = Math.sin(Math.atan2(dy,dx))*speed2;
			crtPoint.x += _x;
			crtPoint.y += _y;
			
			directionLitener();
		}
		/**
		 *更新 怪物的方向
		 * 
		 */		
		private function directionLitener():void
		{
			
			if(dx<0 && Math.abs(dx)>Math.abs(dy))
			{
				direction = 0
			}
			else if ( dy>0 && Math.abs(dy)>Math.abs(dx))
			{
				direction = 1;
			}
			else if ( dx>0 && Math.abs(dx)>Math.abs(dy))
			{
				direction = 2;
			}
			else if ( dy<0 && Math.abs(dy)>Math.abs(dx))
			{
				direction = 3;
			}
		}
		
	}
}